/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package modelo;

import clases.Articulo;
import clases.DetalleOrdenCompra;
import clases.OrdenCompra;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Edder
 */
public class MdlDetalleOrdenCompra implements Modelo<DetalleOrdenCompra>{

    @Override
    public void insert(DetalleOrdenCompra objeto, Conexion cnx) throws SQLException {
        String sql = "INSERT INTO detalleordencompra(idOrdenCompra, idArticulo, cantidad, precio) "
                + "VALUES(" + objeto.getOrdenCompra().getIdOrdenCompra() + ", "
                + objeto.getArticulo().getIdArticulo() + ", "
                + objeto.getCantidad() + ", "
                + objeto.getPrecio() + ");";
        cnx.ejecutar(sql);
    }

    @Override
    public int getLastId(Conexion cnx) throws SQLException {
        String sql = "SELECT MAX(idDetalleOrdenCompra) FROM detalleordencompra";
        ResultSet rs = cnx.consultar(sql);
        if(rs.next())
            return rs.getInt(1);
        return -1;
    }

    @Override
    public void update(DetalleOrdenCompra objeto, Conexion cnx) throws SQLException {
        String sql = "UPDATE detalleordencompra SET "
                + "idOrdenCompra = " + objeto.getOrdenCompra().getIdOrdenCompra() + ", "
                + "idArticulo = " + objeto.getArticulo().getIdArticulo() + ", "
                + "cantidad = " + objeto.getCantidad() + ", "
                + "precio = " + objeto.getPrecio() + " "
                + "WHERE idDetalleOrdenCompra = " + objeto.getIdDetalleOrdenCompra();
        cnx.ejecutar(sql);
    }

    @Override
    public ArrayList<DetalleOrdenCompra> get(String query, Conexion cnx) throws SQLException {
        ArrayList<DetalleOrdenCompra> detalleordencompras = new ArrayList<>();
        ResultSet rs = cnx.consultar(query);
        while(rs.next()){
            detalleordencompras.add(new DetalleOrdenCompra(
                    rs.getInt("idDetalleOrdenCompra"), 
                    new OrdenCompra(){
                        {
                            this.setIdOrdenCompra(rs.getInt("idOrdenCompra"));
                        }
                    }, 
                    new Articulo(){
                        {
                            this.setIdArticulo(rs.getInt("idArticulo"));
                        }
                    }, 
                    rs.getInt("cantidad"), 
                    rs.getFloat("precio"))
            );
        }
        return detalleordencompras;
    }

    @Override
    public DetalleOrdenCompra get(int id, Conexion cnx) throws SQLException {
        String sql = "SELECT * FROM detalleordencompra WHERE idDetalleOrdenCompra = " + id;
        ResultSet rs = cnx.consultar(sql);
        if(rs.next())
            return new DetalleOrdenCompra(
                    rs.getInt("idDetalleOrdenCompra"), 
                    new OrdenCompra(){
                        {
                            this.setIdOrdenCompra(rs.getInt("idOrdenCompra"));
                        }
                    }, 
                    new Articulo(){
                        {
                            this.setIdArticulo(rs.getInt("idArticulo"));
                        }
                    }, 
                    rs.getInt("cantidad"), 
                    rs.getFloat("precio"));
        return null;
    }
    
}
